<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 17/11/27
 * Time: 17:10
 */

namespace App\Http\Controllers\Admin\Permission;


use App\Http\Controllers\Controller;
use App\Service\Permission;
use App\Repositories\Permission as PermissionRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;

class IndexController extends Controller
{
    private $permissionManager;
    private $permissionRepository;
    private $request;
    public function __construct (Permission $permissionManager,PermissionRepository $permissionRepository)
    {
        $this->permissionManager = $permissionManager;
        $this->permissionRepository = $permissionRepository;
    }

    public function index(Request $request){
        $this->request = $request;
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{
            return view('admin.permission.permission.index',[
                'guard' =>  Permission::getGuardAry(),
                'group' =>  Permission::getGroups()
            ]);
        }
    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(Request $request){
        if(!is_writeable(storage_path())){
            throw new \Exception('`storage`文件不可写入');
        }
        return $this->page_json_render($this->permissionManager->getPageList($request));
    }

    /**
     * 编辑页面
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        if(!is_writeable(storage_path())){
            throw new \Exception('`storage`文件不可写入');
        }
        $model = $this->permissionRepository->find($id);
        return view('admin.permission.permission.add-edit',[
            'model' =>  $model,
            'id'    =>  $id,
            'guard' => Permission::getGuardAry(),
            'group' =>  Permission::getGroups()
        ]);
    }


    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){

        if(!is_writeable(storage_path())){
            throw new \Exception('`storage`文件不可写入');
        }
        $permission = Permission::getByGuard_Name('web');
        return view('admin.permission.permission.add-edit',[
            'guard' => Permission::getGuardAry(),
            'group' =>  Permission::getGroups()
        ]);
    }

    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(Request $request){

            $this->validate($request,[
                'name'    =>  'required|filled|max:50',
                'desc'    =>  'required|filled|max:255',
                'guard_name' =>  ['required',Rule::in(Permission::getGuardAry())],
                'group_name'   =>  'required',
            ]);
        $this->permissionManager->insertOrUpdate($request->post());
        Cache::flush();

        return redirect()->route('admin.permission')->with(
            $this->success()
        );
    }

    /**
     * @return array
     */
    private function delete(){
        Cache::flush();
        try{
            $this->permissionManager->del($this->request->id);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }

    private function sortCol(){
        try{
            $this->permissionManager->sortCol($this->request->id,$this->request->sort);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }
}